import time
import hashlib
import hmac
import base64
import json
from urllib.parse import urlencode

class KuaishouSign:
    def __init__(self):
        self.app_key = "3c2cd3f3"
        # 这里需要正确的密钥
        self.app_secret = "your_app_secret"
        
    def get_timestamp(self):
        return str(int(time.time() * 1000))
        
    def generate_signature(self, params):
        """生成签名"""
        # 1. 移除已存在的sig参数
        params = {k: v for k, v in params.items() if k != 'sig'}
        
        # 2. 参数排序
        sorted_params = dict(sorted(params.items()))
        
        # 3. 拼接参数字符串
        param_str = ''.join(f"{k}{v}" for k, v in sorted_params.items())
        
        # 4. 加密处理
        # 注意：这里的具体加密方式需要根据实际情况调整
        hash_obj = hashlib.sha256(param_str.encode('utf-8'))
        signature = base64.b64encode(hash_obj.digest()).decode('utf-8')
        
        return signature
        
    def build_request(self, mobile, country_code="+86"):
        """构建请求参数"""
        params = {
            "mobileCountryCode": country_code,
            "mobile": mobile,
            "type": "27",
            "cs": "false",
            "client_key": self.app_key,
            "videoModelCrowdTag": "1_99",
            "os": "android",
            "timestamp": self.get_timestamp()
        }
        
        # 生成签名
        signature = self.generate_signature(params)
        params["sig"] = signature
        
        return params
        
    def encrypt_data(self, data):
        """加密数据"""
        # 实际使用时需要实现具体的加密算法
        pass
        
    def decrypt_data(self, encrypted_data):
        """解密数据"""
        # 实际使用时需要实现具体的解密算法
        pass

def main():
    ks = KuaishouSign()
    params = ks.build_request("18229650633")
    print("Request Parameters:")
    print(json.dumps(params, indent=2))

if __name__ == "__main__":
    main() 